package com.ruoyi.secondvitim.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.secondvitim.domain.QueSubmitForm;
import com.ruoyi.secondvitim.domain.QueSubmitFormDetail;
import com.ruoyi.secondvitim.domain.SubmitFormResult;
import com.ruoyi.secondvitim.mapper.QueSubmitFormMapper;
import com.ruoyi.secondvitim.service.IQueSubmitFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 提交问卷Service业务层处理
 * 
 * @author alonso
 * @date 2022-02-21
 */
@Service
public class QueSubmitFormServiceImpl implements IQueSubmitFormService
{
    @Autowired
    private QueSubmitFormMapper queSubmitFormMapper;

    /**
     * 查询提交问卷
     *
     * @param id 提交问卷主键
     * @return 提交问卷
     */
    @Override
    public QueSubmitForm selectQueSubmitFormById(Long id)
    {
        return queSubmitFormMapper.selectQueSubmitFormById(id);
    }

    /**
     * 查询提交问卷列表
     * 
     * @param queSubmitForm 提交问卷
     * @return 提交问卷
     */
    @Override
    public List<QueSubmitForm> selectQueSubmitFormList(QueSubmitForm queSubmitForm)
    {
        return queSubmitFormMapper.selectQueSubmitFormList(queSubmitForm);
    }

    /**
     * 新增提交问卷
     * 
     * @param queSubmitForm 提交问卷
     * @return 结果
     */
    @Override
    public int insertQueSubmitForm(QueSubmitForm queSubmitForm)
    {
        queSubmitForm.setCreateTime(DateUtils.getNowDate());
        return queSubmitFormMapper.insertQueSubmitForm(queSubmitForm);
    }

    /**
     * 修改提交问卷
     * 
     * @param queSubmitForm 提交问卷
     * @return 结果
     */
    @Override
    public int updateQueSubmitForm(QueSubmitForm queSubmitForm)
    {
        queSubmitForm.setUpdateTime(DateUtils.getNowDate());
        return queSubmitFormMapper.updateQueSubmitForm(queSubmitForm);
    }

    /**
     * 批量删除提交问卷
     * 
     * @param ids 需要删除的提交问卷主键
     * @return 结果
     */
    @Override
    public int deleteQueSubmitFormByIds(String ids)
    {
        return queSubmitFormMapper.deleteQueSubmitFormByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除提交问卷信息
     * 
     * @param id 提交问卷主键
     * @return 结果
     */
    @Override
    public int deleteQueSubmitFormById(Long id)
    {
        return queSubmitFormMapper.deleteQueSubmitFormById(id);
    }

    @Override
    public SubmitFormResult calSubmitResult(QueSubmitForm queSubmitForm, List<Map<String, Object>> list) {
        SubmitFormResult submitFormResult = new SubmitFormResult();
        //表单id
        submitFormResult.setFormId(queSubmitForm.getFormId());
        //患者治疗阶段
        submitFormResult.setStage(queSubmitForm.getStage());
        //患者手机号
        submitFormResult.setUserId(queSubmitForm.getUserId());
        //问卷类型
        submitFormResult.setType(queSubmitForm.getType());
        submitFormResult = calGradeResult(submitFormResult, list);
        return submitFormResult;
    }

    /**
     * 根据提交选项计算分数
     * */
    private SubmitFormResult calGradeResult(SubmitFormResult submitFormResult, List<Map<String, Object>> list){
        List<QueSubmitFormDetail> queSubmitFormDetailList = new ArrayList<>();
        //问卷类型
        String type = submitFormResult.getType();
        //食管癌症状评估量表
        if(type.equals("2")) {
            //发生频率 有无 有 发生频率 1：极少 4：持续出现
            //严重程度 1：轻度  4：很严重
            //困扰程度： 0：没有困扰  4：很严重
            int scoreA = 0;//发生频率
            int scoreB = 0;//严重程度
            int scoreC = 0;//困扰程度
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);
                String remark = map.get("remark") != null ? map.get("remark").toString() : null;
                //问题
                String optionQuestion = map.get("options").toString();
                //选项对应分数
                int optionValue = Integer.parseInt(map.get("optionValue").toString());
                //选项对应文本描述
                String optionText = map.get("optionText").toString();
                queSubmitFormDetail.setOptionValue(optionValue + "");
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId").toString();
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                if(remark.equals("A")) {
                    scoreA += optionValue;
                } else if(remark.equals("B")){
                    scoreB += optionValue;
                } else if(remark.equals("C")){
                    scoreC += optionValue;
                }

                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            String total = scoreA + "," + scoreB + "," + scoreC;
            submitFormResult.setTotal(total);
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        } else if(type.equals("3")){
            //焦虑抑郁量表
            //A:焦虑因子评分 D：抑郁因子评分
            int scoreA = 0;
            int scoreD = 0;
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);
                String remark = map.get("remark") != null ? map.get("remark").toString() : null;
                //问题
                String optionQuestion = map.get("options").toString();
                //选项对应分数
                int optionValue = Integer.parseInt(map.get("optionValue").toString());
                //选项对应文本描述
                String optionText = map.get("optionText").toString();
                queSubmitFormDetail.setOptionValue(optionValue + "");
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId").toString();
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                    if(remark.equals("A")) {
                    scoreA += optionValue;
                } else if(remark.equals("D")){
                    scoreD += optionValue;
                }

                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            String total = scoreA + "," + scoreD;
            submitFormResult.setTotal(total);
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        } else if(type.equals("4")){
            //社会支持评定量表 1-4 8-10 单选 1-4分 6、7 无0分 其余多选 有几个得几分
            //计分方式remark：总分：条目之和记总分 客观支持分：2、6、7 主观支持分：1、3、4、5 对支持的利用度：8、9、10
            int scoreA = 0;//客观支持分
            int scoreB = 0;//主观支持分
            int scoreC = 0;//支持利用度
            int scoreTotal = 0;//总分
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);
                String remark = map.get("remark") != null ? map.get("remark").toString() : null;
                //问题
                String optionQuestion = map.get("options").toString();
                String optionValueStr = map.get("optionValue").toString();
                queSubmitFormDetail.setOptionValue(optionValueStr);
                //选项对应文本描述
                String optionText = map.get("optionText").toString();
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId").toString();
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                if(remark.equals("2") || remark.equals("6") || remark.equals("7")) {
                    //选项对应分数
                    if(remark.equals("6") || remark.equals("7")) {
                        //多选
                        String[] optionValueSplit = optionValueStr.split(",");
                        for(int k=0,klen=optionValueSplit.length; k<klen; k++){
                            int optionValue = Integer.parseInt(optionValueSplit[k]);
                            if(optionValue > 0) {
                                //无0分  其余有几个得几分
                                scoreA += 1;
                            }
                        }
                    } else {
                        int optionValue = Integer.parseInt(optionValueStr);
                        scoreA += optionValue;
                    }
                } else if(remark.equals("1") || remark.equals("3") || remark.equals("4") || remark.equals("5")){
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreB += optionValue;
                } else if(remark.equals("8") || remark.equals("9") || remark.equals("10")){
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreC += optionValue;
                }

                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            scoreTotal = scoreA + scoreB + scoreC;
            String total = scoreA + "," + scoreB + "," + scoreC + "," + scoreTotal;
            submitFormResult.setTotal(total);
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        } else if(type.equals("5") || type.equals("7")){
            //癌症自我管理效能感量表
            int score = 0;
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);
                String remark = map.get("remark") != null ? map.get("remark").toString() : null;
                //问题
                String optionQuestion = map.get("options").toString();
                //选项对应分数
                int optionValue = Integer.parseInt(map.get("optionValue").toString());
                //选项对应文本描述
                String optionText = map.get("optionText").toString();
                queSubmitFormDetail.setOptionValue(optionValue + "");
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId").toString();
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                score += optionValue;
                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            submitFormResult.setTotal(score + "");
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        } else if(type.equals("6")) {
            //自我护理能力测定量表
            //自我概念(1-8)、自护责任感(9-14)、自我护理技能(15-26)、健康知识水平(27-43)
            int scoreA = 0;//自我概念
            int scoreB = 0;//自护责任感
            int scoreC = 0;//自我护理技能
            int scoreD = 0;//健康知识水平
            int scoreTotal = 0;//总分
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);

                String remark = map.get("remark") != null ? map.get("remark").toString() : "";
                //问题
                String optionQuestion = map.get("options") != null ? map.get("options").toString() : "";
                String optionValueStr = map.get("optionValue") != null ? map.get("optionValue").toString() : "";
                queSubmitFormDetail.setOptionValue(optionValueStr);
                //选项对应文本描述
                String optionText = map.get("optionText") != null ? map.get("optionText").toString() : "";
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId") != null ? map.get("queFormDetailOptionId").toString() : "";
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                if(remark.equals("A")) {
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreA += optionValue;
                } else if(remark.equals("B")){
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreB += optionValue;
                } else if(remark.equals("C")){
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreC += optionValue;
                } else if(remark.equals("D")){
                    //选项对应分数
                    int optionValue = Integer.parseInt(optionValueStr);
                    queSubmitFormDetail.setOptionValue(optionValue + "");
                    scoreD += optionValue;
                }

                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            scoreTotal = scoreA + scoreB + scoreC + scoreD;
            String total = scoreA + "," + scoreB + "," + scoreC + "," + scoreD + "," + scoreTotal;
            submitFormResult.setTotal(total);
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        } else {
            //其它类型表单
            for(int i=0,len=list.size(); i<len; i++){
                QueSubmitFormDetail queSubmitFormDetail = new QueSubmitFormDetail();
                Map<String, Object> map = list.get(i);
                String remark = map.get("remark") != null ? map.get("remark").toString() : null;
                //问题
                String optionQuestion = map.get("options") != null ? map.get("options").toString() : null;
                //选项对应分数
                String optionValue = map.get("optionValue") != null ? map.get("optionValue").toString() : null;
                //选项对应文本描述
                String optionText = map.get("optionText") != null ? map.get("optionText").toString() : null;
                queSubmitFormDetail.setOptionValue(optionValue);
                queSubmitFormDetail.setOptionText(optionText);
                queSubmitFormDetail.setOptions(optionQuestion);
                //提交成功的formId
                queSubmitFormDetail.setSubmitFormId(submitFormResult.getFormId());
                //选项id
                String queFormDetailOptionId = map.get("queFormDetailOptionId") != null ? map.get("queFormDetailOptionId").toString() : null;
                queSubmitFormDetail.setOptionFormId(queFormDetailOptionId);
                queSubmitFormDetailList.add(queSubmitFormDetail);
            }
            submitFormResult.setQueSubmitFormDetailList(queSubmitFormDetailList);
        }

        return submitFormResult;
    }
}
